<?php
namespace app\index\controller;
use think\Controller;

class soap extends Controller
{
    public function index()
    {
        header("content-type:text/html;charset=utf-8");
        try {
            $client = new \SoapClient("http://api.whcmlabs.com/CMLABSReportWebService.asmx?WSDL",
                array(
                    "stream_context" => stream_context_create(
                        array(
                            'ssl' => array(
                                'verify_peer' => false,
                                'verify_peer_name' => false,
                            )
                        )
                    )
                )
            );
           /*print_r($client->__getFunctions());
           exit();*/
            //print_r($client->__getTypes());
            //$parm1 = array('ybid' => '616056003497', 'Type' => 'PJ');  更新样本状态
            /*$parm = array('cmlabsBarCode' => '614010025167', 'key' => 'fda8469c-820c-4775-aac8-d475de26b105');

            $result = $client->GetReportItemListByCmlabsBarcode($parm);*/
            $parm = array('hospitalcode' => '614010', 'Type' => 'PJ');
            $result = $client->GetHosptaSelectData($parm);

            //数据转换  object -> array
            function object_array($array) {
                if(is_object($array)) {
                    $array = (array)$array;
                } if(is_array($array)) {
                    foreach($array as $key=>$value) {
                        $array[$key] = object_array($value);
                    }
                }
                return $array;

            }
            $data = (object_array($result))['GetHosptaSelectDataResult']['HosPtalLst'];

            var_dump($data);
            foreach ($data as $key=>$vales){
                var_dump($vales['CmlabsCode']);
            }

            $result = $data['GetReportItemListByCmlabsBarcodeResult']['Data']['enc_value']['ApplyItemResult'];
            unset($result['TechnicianSign']);  //去掉签名 乱码数据
            unset($result['CheckerSign']);
            if(empty($result)){
                echo '没有该条码的结果值';
            }
            //将stdclass object的$result转换为array
           /* $result = get_object_vars($result);
            //输出结果
            echo $result["age"];*/

        } catch (SOAPFault $e) {
            print $e;
        }
       // ('2020-01-03', '2020-08-22', 'ce2c6f15-1150-4fa6-8831-d48287600b01'))  614010018367   ;
        //拼接 xml
        $xml = "
<?xml version=\"1.0\" encoding=\"gb2312\" standalone=\"no\"?>
<result>
//-----------------多条记录如此反复
<resultdetail>
<barcode>20170920004231</barcode>
<jyys>张三</jyys>
<shys>李四</shys>
<shsj>2017-09-21 15:30</shsj>
<desccode>300049</desccode>
<xmdh>AAA</xmdh>
<xmmc>测试</xmmc>
<jyjg>250</jyjg>
<gdbz>H</gdbz>
<xmdw>cm</xmdw>
<xx>1</xx>
<sx>200</sx>
<xmckz>1-200</xmckz>
</resultdetail>
</result> 
";
    }

    /*
     * @param $urls根据地址返回对应的  $client
     */
    public function ReturnClientByUrl($url)
    {
        libxml_disable_entity_loader(false);
        $opts = array(
            'http' => array(
                'user_agent' => 'PHPSoapClient'
            ),
            'ssl' => array(
                'verify_peer' => false
            ),
        );
        $context = stream_context_create($opts);

        $requestUrl = $url;
        //捕获下异常
        try {
            $client = new \SoapClient($requestUrl, array("trace" => 1, "cache_wsdl" => WSDL_CACHE_NONE, "exception" => 1, 'stream_context' => $context));
        } catch (Exception $e) {
            die($e->getMessage());
        }

        return $client;
    }
}